package com.bocloud.cmp.driver.providers.os;

import java.util.List;

import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.storage.block.VolumeSnapshot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bocloud.cmp.driver.providers.SnapshotDriver;
import com.bocloud.cmp.model.SnapshotModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;

public class OSSnapshotDriver extends OSDriver implements SnapshotDriver {

	private static Logger logger = LoggerFactory.getLogger(OSSnapshotDriver.class);

	/**
	 * 
	 */
	public OSSnapshotDriver() {
		super();
	}

	public OSSnapshotDriver(String endpoint, String username, String password, String tenant, String domain,
			String project, Version version) {
		super(endpoint, username, password, tenant, domain, project, version);
	}

	@Override
	public BsmResult list() {
		List<SnapshotModel> models = Lists.newArrayList();
		List<? extends VolumeSnapshot> snapshots = Lists.newArrayList();
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		snapshots = this.getClient().blockStorage().snapshots().list();
		if (null != snapshots) {
			for (VolumeSnapshot snapshot : snapshots) {
				models.add(convert(snapshot));
			}
		}
		return new BsmResult(true, models, "success");
	}

	@Override
	public BsmResult remove(SnapshotModel model) {
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		ActionResponse response = this.getClient().blockStorage().snapshots().delete(model.getSnapshotId());
		if (response.isSuccess()) {
			return new BsmResult(true, "删除成功!");
		} else {
			logger.error("remove snapshot [{}] error: {}", model.getSnapshotId(), response.getFault());
			return new BsmResult(false, response.getFault());
		}
	}

	private SnapshotModel convert(VolumeSnapshot snapshot) {
		if (null == snapshot) {
			return null;
		}
		SnapshotModel model = new SnapshotModel();
		model.setGmtCreate(snapshot.getCreated());
		model.setName(snapshot.getName());
		model.setSize(snapshot.getSize());
		model.setSnapshotId(snapshot.getId());
		model.setVolumeId(snapshot.getVolumeId());
		model.setStatus(snapshot.getStatus().name());
		return model;
	}

	@Override
	public BsmResult modify(SnapshotModel model) {
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		ActionResponse response = this.getClient().blockStorage().snapshots().update(model.getSnapshotId(),
				model.getName(), model.getRemark());
		if (response.isSuccess()) {
			return new BsmResult(true, "success");
		} else {
			return new BsmResult(false, response.getFault());
		}

	}

	@Override
	public BsmResult detail(SnapshotModel model) {
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		VolumeSnapshot snapshot = this.getClient().blockStorage().snapshots().get(model.getSnapshotId());
		if (null == snapshot) {
			logger.error("snapshot [{}] doesnot exsit!", model.getSnapshotId());
			return new BsmResult(false, "快照不存在！");
		}
		return new BsmResult(true, convert(snapshot), "success");
	}

	@Override
	public BsmResult revert(SnapshotModel model) {
		// TODO Auto-generated method stub
		return null;
	}

}
